package com.luca.sl651server.server;

import com.luca.sl651server.enums.ServerRunModeEnum;
import com.luca.sl651server.server.config.NettyServerConfig;
import com.luca.sl651server.server.tcp.TcpNettyServer;
import com.luca.sl651server.server.udp.UdpNettyServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class ServerStart implements CommandLineRunner {

    @Autowired
    private NettyServerConfig config;


    @Override
    public void run(String... args) throws Exception {
        String serverRunMode = config.getServerRunMode();
        ServerRunModeEnum runModeEnum = ServerRunModeEnum.getServerRunModeEnum(serverRunMode);
        if (runModeEnum == null) {
            throw new Exception("un support server run mod " + serverRunMode);
        }
        switch (runModeEnum) {
            case SERVER_MODE_TCP:
                new TcpNettyServer(config).run();
                break;
            case SERVER_MODE_UDP:
                new UdpNettyServer(config).run();
                break;
            default:
                throw new Exception("un support server run mod " + serverRunMode);
        }
    }
}
